/*
 * Matrix_operations.java
 *
 * Created on __DATE__, __TIME__
 */

package main;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.table.DefaultTableModel;

import utils.Contents;
import utils.MessageBox;
import utils.SciUtils;
import utils.StringUtils;


/**
 *
 * @author  __USER__
 */
public class Matrix_operations extends JFrame {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	/** Creates new form Matrix_operations */
	private Matrix_operations() {
		initComponents();
		setDefaultCloseOperation(HIDE_ON_CLOSE);
	}
	private static Matrix_operations instance;
	public static Matrix_operations getInstance(){
		if(instance==null){
			instance =  new Matrix_operations();
		}
		return instance;
	}
	/** This method is called from within the constructor to
	 * initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is
	 * always regenerated by the Form Editor.
	 */
	//GEN-BEGIN:initComponents
	// <editor-fold defaultstate="collapsed" desc="Generated Code">
	private void initComponents() {
		ImageIcon icon=new ImageIcon(Contents.ICON_PATH);//
		setIconImage(icon.getImage());
		getContentPane().setBackground(new Color(199, 191, 230));
		jScrollPane1 = new javax.swing.JScrollPane();
		jTable1 = new javax.swing.JTable();
		jScrollPane2 = new javax.swing.JScrollPane();
		jTable2 = new javax.swing.JTable();
		jComboBox1 = new javax.swing.JComboBox();
		jButton1 = new javax.swing.JButton();
		jLabel1 = new javax.swing.JLabel();
		jRadioButton1 = new javax.swing.JRadioButton();
		jRadioButton2 = new javax.swing.JRadioButton();
		jTextField1 = new javax.swing.JTextField();
		jRadioButton3 = new javax.swing.JRadioButton();
		jRadioButton4 = new javax.swing.JRadioButton();
		jRadioButton5 = new javax.swing.JRadioButton();
		jScrollPane3 = new javax.swing.JScrollPane();
		jTable3 = new javax.swing.JTable();
		setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
		setTitle("矩阵运算：操作演示");
		objectarray = new Object[][] { { null, null, null }, { null, null, null },
				{ null, null, null } };
		stringArray = new String[]{"第一列","第二列","第三列" };
		 t1 = new javax.swing.table.DefaultTableModel(
				 objectarray,stringArray);
		 t2 = new javax.swing.table.DefaultTableModel(
				 objectarray,stringArray);
		 t3 = new javax.swing.table.DefaultTableModel(
				 objectarray,stringArray);
		jTable1.setModel(t1);
		jScrollPane1.setViewportView(jTable1);

		jTable2.setModel(t2);
		jScrollPane2.setViewportView(jTable2);

		jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] {
				"+", "-", "*" }));

		jButton1.setText("\u7b49\u4e8e");
		jButton1.addActionListener(button_Ok_AL);
		jLabel1
				.setText("矩阵运算的简单操作");

		jRadioButton1.setText("\u77e9\u9635\u8fd0\u7b97");
		jRadioButton1.addActionListener(radioButton_AL);
		jRadioButton2.setText("\u6570\u4e58");
		jRadioButton2.addActionListener(radioButton_AL);
		jRadioButton3.setText("\u6c42\u9006");
		jRadioButton3.addActionListener(radioButton_AL);
		jRadioButton4.setText("\u6c42\u79e9");
		jRadioButton4.addActionListener(radioButton_AL);
		jRadioButton5.setText("\u8f6c\u7f6e");
		jRadioButton5.addActionListener(radioButton_AL);
		jTable3.setModel(t3);
		jTable3.setEnabled(false);
		jScrollPane3.setViewportView(jTable3);

		javax.swing.GroupLayout layout = new javax.swing.GroupLayout(
				getContentPane());
		getContentPane().setLayout(layout);
		layout
				.setHorizontalGroup(layout
						.createParallelGroup(
								javax.swing.GroupLayout.Alignment.LEADING)
						.addGroup(
								layout
										.createSequentialGroup()
										.addContainerGap()
										.addGroup(
												layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.LEADING)
														.addGroup(
																layout
																		.createSequentialGroup()
																		.addComponent(
																				jLabel1,
																				javax.swing.GroupLayout.PREFERRED_SIZE,
																				423,
																				javax.swing.GroupLayout.PREFERRED_SIZE)
																		.addContainerGap(
																				249,
																				Short.MAX_VALUE))
														.addGroup(
																layout
																		.createSequentialGroup()
																		.addComponent(
																				jScrollPane1,
																				javax.swing.GroupLayout.PREFERRED_SIZE,
																				261,
																				javax.swing.GroupLayout.PREFERRED_SIZE)
																		.addPreferredGap(
																				javax.swing.LayoutStyle.ComponentPlacement.RELATED)
																		.addGroup(
																				layout
																						.createParallelGroup(
																								javax.swing.GroupLayout.Alignment.LEADING)
																						.addComponent(
																								jComboBox1,
																								0,
																								106,
																								Short.MAX_VALUE)
																						.addComponent(
																								jRadioButton1))
																		.addPreferredGap(
																				javax.swing.LayoutStyle.ComponentPlacement.RELATED)
																		.addComponent(
																				jScrollPane2,
																				javax.swing.GroupLayout.PREFERRED_SIZE,
																				279,
																				javax.swing.GroupLayout.PREFERRED_SIZE)
																		.addGap(
																				163,
																				163,
																				163))
														.addGroup(
																layout
																		.createSequentialGroup()
																		.addGroup(
																				layout
																						.createParallelGroup(
																								javax.swing.GroupLayout.Alignment.LEADING)
																						.addGroup(
																								layout
																										.createSequentialGroup()
																										.addComponent(
																												jRadioButton3)
																										.addPreferredGap(
																												javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
																										.addComponent(
																												jRadioButton4)
																										.addPreferredGap(
																												javax.swing.LayoutStyle.ComponentPlacement.RELATED)
																										.addComponent(
																												jRadioButton5))
																						.addComponent(
																								jButton1,
																								javax.swing.GroupLayout.DEFAULT_SIZE,
																								151,
																								Short.MAX_VALUE))
																		.addPreferredGap(
																				javax.swing.LayoutStyle.ComponentPlacement.RELATED)
																		.addGroup(
																				layout
																						.createParallelGroup(
																								javax.swing.GroupLayout.Alignment.LEADING)
																						.addComponent(
																								jScrollPane3,
																								javax.swing.GroupLayout.PREFERRED_SIZE,
																								279,
																								javax.swing.GroupLayout.PREFERRED_SIZE)
																						.addGroup(
																								layout
																										.createSequentialGroup()
																										.addComponent(
																												jRadioButton2,
																												javax.swing.GroupLayout.PREFERRED_SIZE,
																												66,
																												javax.swing.GroupLayout.PREFERRED_SIZE)
																										.addPreferredGap(
																												javax.swing.LayoutStyle.ComponentPlacement.RELATED)
																										.addComponent(
																												jTextField1,
																												javax.swing.GroupLayout.PREFERRED_SIZE,
																												91,
																												javax.swing.GroupLayout.PREFERRED_SIZE)))
																		.addGap(
																				386,
																				386,
																				386)))));
		layout
				.setVerticalGroup(layout
						.createParallelGroup(
								javax.swing.GroupLayout.Alignment.LEADING)
						.addGroup(
								layout
										.createSequentialGroup()
										.addContainerGap()
										.addComponent(
												jLabel1,
												javax.swing.GroupLayout.PREFERRED_SIZE,
												25,
												javax.swing.GroupLayout.PREFERRED_SIZE)
										.addGap(8, 8, 8)
										.addGroup(
												layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.LEADING)
														.addGroup(
																layout
																		.createSequentialGroup()
																		.addComponent(
																				jComboBox1,
																				javax.swing.GroupLayout.PREFERRED_SIZE,
																				javax.swing.GroupLayout.DEFAULT_SIZE,
																				javax.swing.GroupLayout.PREFERRED_SIZE)
																		.addPreferredGap(
																				javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
																		.addComponent(
																				jRadioButton1))
														.addGroup(
																layout
																		.createParallelGroup(
																				javax.swing.GroupLayout.Alignment.LEADING,
																				false)
																		.addComponent(
																				jScrollPane1,
																				javax.swing.GroupLayout.Alignment.TRAILING,
																				0,
																				0,
																				Short.MAX_VALUE)
																		.addComponent(
																				jScrollPane2,
																				javax.swing.GroupLayout.Alignment.TRAILING,
																				javax.swing.GroupLayout.DEFAULT_SIZE,
																				164,
																				Short.MAX_VALUE)))
										.addGap(42, 42, 42)
										.addGroup(
												layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.BASELINE)
														.addComponent(
																jRadioButton3)
														.addComponent(
																jRadioButton4)
														.addComponent(
																jRadioButton5)
														.addComponent(
																jRadioButton2)
														.addComponent(
																jTextField1,
																javax.swing.GroupLayout.PREFERRED_SIZE,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																javax.swing.GroupLayout.PREFERRED_SIZE))
										.addPreferredGap(
												javax.swing.LayoutStyle.ComponentPlacement.RELATED)
										.addGroup(
												layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.LEADING)
														.addComponent(
																jScrollPane3,
																javax.swing.GroupLayout.PREFERRED_SIZE,
																164,
																javax.swing.GroupLayout.PREFERRED_SIZE)
														.addComponent(jButton1))
										.addGap(30, 30, 30)));
		//将radio button放到一组
		ButtonGroup group = new ButtonGroup();
		group.add(jRadioButton1);
		group.add(jRadioButton2);
		group.add(jRadioButton3);
		group.add(jRadioButton4);
		group.add(jRadioButton5);
		pack();
	}// </editor-fold>
	//GEN-END:initComponents

	/**
	 * radion button 监听
	 *
	 * */
	ActionListener radioButton_AL = new ActionListener() {
		public void actionPerformed(ActionEvent ae) {
			if(ae.getSource().equals(jRadioButton1)){
//				MessageBox.Show("第一个radion，计算矩阵运算");
			}else if(ae.getSource().equals(jRadioButton2)){
				if(jTextField1.getText()==null)
					jTextField1.setFocusable(true);
//				MessageBox.Show("第二个radion，计算数乘");
			}else if(ae.getSource().equals(jRadioButton3)){
//				MessageBox.Show("第三个radion，求逆");
			}else if(ae.getSource().equals(jRadioButton4)){
//				MessageBox.Show("第四个radion，求秩");
			}
			else if(ae.getSource().equals(jRadioButton5)){
//				MessageBox.Show("第五个radion，转置");
			}
//			MessageBox.Show("点击"+);
		}
	};
	ActionListener button_Ok_AL = new ActionListener() {
		
		public void actionPerformed(ActionEvent e) {
			
			if(jRadioButton1.isSelected()){
				for(int i=0 ;i<t1.getColumnCount();i++){
					for(int j=0;j<t1.getRowCount();j++){
						String value=t1.getValueAt(j, i)==null?"0": t1.getValueAt(j, i).toString();
						String value2 = t2.getValueAt(j, i)==null?"0": t2.getValueAt(j, i).toString();
						Float f = new Float(0);
						Float f2 = new Float(0);
						if(StringUtils.isNumber(value) || StringUtils.isNumber(value2)){
							f = Float.parseFloat(value);
							f2 = Float.parseFloat(value2);
							switch(jComboBox1.getSelectedIndex()){
								case 0:
									f=f+f2;
									break;
								case 1:
									f=f-f2;
								break;	
								case 2:
									f=f*f2;
								break;
							}
						}
						t3.setValueAt(f, j, i);
					}
				}
			}else if(jRadioButton2.isSelected()){
				// 计算数乘 
				if(!jTextField1.getText().isEmpty() && StringUtils.isNumber(jTextField1.getText())){
					for(int i=0 ;i<jTable1.getModel().getColumnCount();i++){
						for(int j=0;j<jTable2.getModel().getRowCount();j++){
							String value= jTable1.getModel().getValueAt(i, j)==null?"0":jTable1.getModel().getValueAt(i, j).toString();
							if(StringUtils.isNumber(value)){
								Float f = Float.parseFloat(value);
								jTable3.getModel().setValueAt(f*Float.parseFloat(jTextField1.getText()), i, j);
							}else {
								jTable3.getModel().setValueAt(0, i, j);
							}
						}
					}
					jTable3.setVisible(true);	
				}else
				MessageBox.Show("数乘计算的输入框为空，或不是数字！");
				jTextField1.setFocusable(true);
				
			}else if(jRadioButton3.isSelected()){
				// 求逆 0
				System.out.println(StringUtils.format4Table2sci(t1));
				if(SciUtils.sciEngin(StringUtils.format4Table2sci(t1),0)!=null){
					t3.setDataVector(SciUtils.sciEngin(StringUtils.format4Table2sci(t1),0), new String[] {"第一列","第二列","第三列", "第四列"});
					jTable3.setVisible(true);
				}else {
					MessageBox.Show("警告：矩阵接近奇异或病态！");
				}
				
				
			}else if(jRadioButton4.isSelected()){
				//MessageBox.Show("第四个radion，求秩");
				System.out.println(StringUtils.format4Table2sci(t1));
				if(SciUtils.sciEngin(StringUtils.format4Table2sci(t1),0)!=null){
					MessageBox.Show("矩阵的秩为："+SciUtils.sciEngin(StringUtils.format4Table2sci(t1), 1)[0][0]);
					jTable3.setVisible(true);
				}else {
					MessageBox.Show("警告：矩阵接近奇异或病态！");
				}
			}else if(jRadioButton5.isSelected()){
				//转置 2
				for(int i=0 ;i<jTable1.getModel().getColumnCount();i++){
					for(int j=0;j<jTable1.getModel().getRowCount();j++){
						jTable3.getModel().setValueAt(jTable1.getModel().getValueAt(j, i), i, j);
					}
				}
				
			} else MessageBox.Show("请选择运算类型！");   
		}
	};
	
	
	/**
	 * @param args the command line arguments
	 */
//	public static void main(String args[]) {
//		java.awt.EventQueue.invokeLater(new Runnable() {
//			public void run() {
//				new Matrix_operations().setVisible(true);
//			}
//		});
//	}

	//GEN-BEGIN:variables
	// Variables declaration - do not modify
	private javax.swing.JButton jButton1;
	private javax.swing.JComboBox jComboBox1;
	private javax.swing.JLabel jLabel1;
	private javax.swing.JRadioButton jRadioButton1;
	private javax.swing.JRadioButton jRadioButton2;
	private javax.swing.JRadioButton jRadioButton3;
	private javax.swing.JRadioButton jRadioButton4;
	private javax.swing.JRadioButton jRadioButton5;
	private javax.swing.JScrollPane jScrollPane1;
	private javax.swing.JScrollPane jScrollPane2;
	private javax.swing.JScrollPane jScrollPane3;
	private javax.swing.JTable jTable1;
	private javax.swing.JTable jTable2;
	private javax.swing.JTable jTable3;
	private javax.swing.JTextField jTextField1;
	private DefaultTableModel t1;
	private DefaultTableModel t2;
	private DefaultTableModel t3;
	private Object[][] objectarray;
	private String[] stringArray;
	// End of variables declaration//GEN-END:variables

}